package com.liji.takephoto;

import android.app.Activity;
import android.content.Context;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.support.annotation.DrawableRes;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

/**
 * Created by codeest on 2016/8/2.
 */
public class ImageLoader {
    
    public static void load(Context context, String url, ImageView iv) { //使用Glide加载圆形ImageView(如头像)时，不要使用占位图
        Glide.with(context).load(url).crossFade().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(iv);
    }
    
    /**
     * 加载本地图片
     * @param context
     * @param resId
     * @param imageView
     */
    public static void loadLocal(Context context, int resId, ImageView imageView) {
        Glide.with(context).load(resId).into(imageView);
    }
    
    public static void load(Context context, String url, ImageView iv, @DrawableRes int placeholder) { //使用Glide加载圆形ImageView(如头像)时，不要使用占位图
        Glide.with(context)
                .load(url)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(placeholder)
                .into(iv);
    }
    
    public static void load(Activity activity, String url, ImageView iv) { //使用Glide加载圆形ImageView(如头像)时，不要使用占位图
        if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
            if (!activity.isDestroyed()) {
                Glide.with(activity).load(url).crossFade().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(iv);
            }
        }
        else {
            Glide.with(activity).load(url).crossFade().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(iv);
        }
    }
    
    public static void loadAll(Context context, String url, ImageView iv) { //不缓存，全部从网络加载
        Glide.with(context)
                .load(url)
                .crossFade()
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(iv);
    }
    
    public static void loadAll(Activity activity, String url, ImageView iv) { //不缓存，全部从网络加载
        if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
            if (!activity.isDestroyed()) {
                Glide.with(activity)
                        .load(url)
                        .crossFade()
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .into(iv);
            }
        }
        else {
            Glide.with(activity)
                    .load(url)
                    .crossFade()
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(iv);
        }
    }
    
    public static void clear(Context context, View view) {
        Glide.clear(view);
    }
}
